using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace gears
{
    class Player
    {
        private Animation player_animation;
        private List<PlayerControls> player_controls = new List<PlayerControls>();
        private List<PlayerInventory> player_inventory = new List<PlayerInventory>();
        private Vector2 player_position = new Vector2(700, 50);
        public Vector2 moveSpeed = new Vector2(0, 0);
        private const int PLAYER_MOVE_SPEED = 2;
        private const int PLAYER_JUMP_SPEED = 8;
        private const float GRAVITY = 0.2f;
        private const int JUMP_HEIGHT = 100;
        private bool jumping = false;
        private bool collideWithMechanism = false;
        private bool sideCollide = false;
        private static bool player_activity = true;
        private static bool player_live = true;
        private string direction = "left";

        public Player()
        {
            player_animation = new Animation(ContentLoader.LoadTexture("player_sprites/player_sprite"),8,7,10);

            player_controls.Add(new PlayerControls(ContentLoader.LoadTexture("left_control"),new Vector2(620,410),"left"));
            player_controls.Add(new PlayerControls(ContentLoader.LoadTexture("right_control"), new Vector2(720, 410), "right"));
            player_controls.Add(new PlayerControls(ContentLoader.LoadTexture("jump_control"), new Vector2(670, 330), "jump"));
        }
        public void Update(GameTime game_time)
        {  
            player_animation.CurrentRow = 2;
            player_animation.Animating();
            PlayerControl();
        }
        public void DrawPlayer(SpriteBatch sprite_batch)
        {     
             
             if (player_activity == true)
             {
                player_animation.DrawAnimation(sprite_batch, player_position);
                if (collideWithMechanism)
                    return;
                foreach (PlayerControls pc in player_controls)
                {
                    pc.DrawPlayerControls(sprite_batch);
                }
            }
        }
     
        public void PlayerControl()
        {
               var tap_positions = TapHandler.GetTouches();
               foreach (PlayerControls p_c in player_controls)
               {
                   p_c.Update();
               }

               if(player_activity==true && !collideWithMechanism)
               {
                   string pressed_control = "";

                   foreach (Vector2 tap_position in tap_positions)
                   {
                       if (tap_positions.Count > 0)
                       {                     
                           foreach (PlayerControls pc in player_controls)
                           {
                               pressed_control = pc.CheckPressed(tap_position);
                               if ((pressed_control == "left" && direction != "coll_left") || 
                                   (jumping==true && pressed_control == "left"))
                               {
                                   if (!sideCollide)
                                   {
                                       player_animation.CurrentRow = 0;
                                       player_animation.Animating();
                                       GoLeft();
                                       direction = "left";

                                   }
                               }
                               else if ((pressed_control == "right" && direction != "coll_right" ||
                                         (jumping == true && pressed_control == "right")))
                               {
                                   if (!sideCollide)
                                   {
                                       player_animation.CurrentRow = 1;
                                       player_animation.Animating();
                                       GoRight();
                                       direction = "right";

                                   }
                               }
                               

                               if (pressed_control == "jump")
                               {
                                   if (jumping == false)
                                   {
                                       jumping = true;
                                       moveSpeed.Y = -PLAYER_JUMP_SPEED;
                                       Game1.mySoundEffects.Play("jump");
                                   }

                               }
                           }
                        
                       }
                   }
               }
               if (direction == "right")
                   {
                       if (moveSpeed.Y < 0)
                       {
                           player_animation.CurrentRow = 3;
                           player_animation.Animating();
                       }
                       else if (moveSpeed.Y > 0)
                       {
                           player_animation.CurrentRow = 5;
                           player_animation.Animating();
                       }
                   }
               else if (direction == "left")
               {
                   if (moveSpeed.Y < 0)
                   {
                       player_animation.CurrentRow = 4;
                       player_animation.Animating();
                   }
                   else if (moveSpeed.Y > 0)
                   {
                       player_animation.CurrentRow = 6;
                       player_animation.Animating();
                   }
               }
               player_position += moveSpeed;
               moveSpeed.Y += GRAVITY;
        }
        
        public void CollideWithPlatform(Vector2 platformPosition, Vector2 platformDimensions, bool _trap)
        {
            sideCollide = false;
            if (moveSpeed.Y == 0 && direction != "")
            {
                if (direction == "left" || direction=="coll_left")
                {
                    player_position = new Vector2(player_position.X + PLAYER_MOVE_SPEED, player_position.Y);
                    direction = "coll_left";
                }
                else if (direction == "right" || direction == "coll_right")
                {
                    player_position = new Vector2(player_position.X - PLAYER_MOVE_SPEED, player_position.Y);
                    direction = "coll_right";
                }
            }
            else if(jumping==true)
            {
                if (direction == "left" || direction == "coll_left")
                {
                    player_position = new Vector2(player_position.X + PLAYER_MOVE_SPEED, player_position.Y);
                }
                else if (direction == "right" || direction == "coll_right")
                {
                    player_position = new Vector2(player_position.X - PLAYER_MOVE_SPEED, player_position.Y);
                }
            }
            if (player_position.Y + (float)player_animation.SpriteHeight - moveSpeed.Y - platformPosition.Y < 1)
            {
                moveSpeed.X = 0;
                moveSpeed.Y = 0;
                player_position.Y = platformPosition.Y - player_animation.SpriteHeight + 1;
                jumping = false;

                if (_trap == true)
                {
                    Player.PlayerLive = false;
                    player_live = false;
                    Level.current_mech = 1;
                    Game1.mySoundEffects.Play("death");
                }
            }
            else if (player_position.Y - moveSpeed.Y < platformPosition.Y + platformDimensions.Y/2)
            {
                moveSpeed.X = 0;
                sideCollide = true;
            }
            else
            {
                moveSpeed.Y = 0;
                player_position.Y = platformPosition.Y + platformDimensions.Y;
            }

        }

        public void CollideWithMechanism(Mechanism mech)
        {
            collideWithMechanism = false;
            if (!mech.Done)
                collideWithMechanism = true;
        }

        public bool PlayerInventoryContainItem(List<string> items)
        {
            if (items.Count > 0)
            {
                foreach (PlayerInventory p_i in player_inventory)
                {
                    if (items.Contains<string>(p_i.ItemName) == true)
                    {
                        return true;
                    }
                }
            }
            else
            {
                return true;
            }
            return false;
            
        }

        private void GoLeft()
        {
            if (player_position.X > 0)
            {
                player_position.X -= PLAYER_MOVE_SPEED;
            }
        }

        private void GoRight()
        {
            if (player_position.X < 800 - player_animation.SpriteWidth)
            {
                player_position.X += PLAYER_MOVE_SPEED;
            }
        }

        public static bool PlayerActivity
        {
            get
            {
                return player_activity;
            }
            set
            {
                player_activity = value;
            }
        }

        public static bool PlayerLive
        {
            get
            {
                return player_live;
            }
            set
            {
                player_live = value;
            }
        }

        public Animation PlayerAnimation
        {
            get
            {
                return player_animation;
            }
        }

        public Vector2 PlayerPosition
        {
            get
            {
                return player_position;
            }
            set
            {
                direction = "left";
                
                player_position = value;
            }
        }

        public int PlayerMoveSpeed
        {
            get
            {
                return PLAYER_MOVE_SPEED;
            }
        }

        public bool CollisionWithMechanism
        {
            get
            {
                return collideWithMechanism;
            }
            set
            {
                collideWithMechanism = value;
            }
        }

        public List<PlayerInventory> PlayerInventory
        {
            get
            {
                return player_inventory;
            }
            set
            {
                player_inventory = value;
            }
        }

        public string Direction
        {
            set
            {
                direction = value;
            }
        }

    }

    class PlayerControls
    {
        private Texture2D control_sprite;
        string control_function;
        private Vector2 control_position;
        private Color color_control = Color.White;

        public PlayerControls(Texture2D _control_sprite, Vector2 _position, string _function)
        {
            control_sprite = _control_sprite;
            control_position = _position;
            control_function = _function;
        }
        public void Update()
        {
            color_control = Color.White;
        }

        public void DrawPlayerControls(SpriteBatch sprite_batch)
        {
            sprite_batch.Draw(control_sprite, control_position, color_control);
        }
        public string CheckPressed(Vector2 possition)
        {

            Rectangle tap_rect = new Rectangle((int)possition.X, (int)possition.Y, 20, 20);
            Rectangle control_rect = new Rectangle((int)control_position.X, (int)control_position.Y, control_sprite.Width, control_sprite.Height);

            if (tap_rect.Intersects(control_rect))
            {
                color_control = Color.Green;

                return control_function;
            }
            color_control = Color.White;
            return "";
        }
    }

    class PlayerInventory
    {
        private string item_name;

        public PlayerInventory(string _item)
        {
            item_name = _item;
        }

        public string ItemName
        {
            get
            {
                return item_name;
            }
            set
            {
                item_name = value;
            }
        }
    }
}
